//
//  xsAuthorityBackGroundStyleCollectionView.m
//  digital_robot_demo
//
//  Created by xskj on 2024/9/19.
//

#import "xsAuthorityBackGroundStyleCollectionView.h"

static NSString *const cellID = @"cellID";

@interface xsAuthorityBackGroundStyleCollectionView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong)NSArray *xsImgageUrlArray;
@property (nonatomic,assign)CGSize cellSize9_16;

@end

@implementation xsAuthorityBackGroundStyleCollectionView


- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout withDataArray:(NSArray *)imgageUrlArray{
    
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if(self){
        self.delegate = self;
        self.dataSource = self;
        [self getSize9_16];
        self.backgroundColor = UIColor.clearColor;//COLOR_VCViewBgcolor;
        
  
        
        [self registerClass:[PhotoCell class] forCellWithReuseIdentifier:cellID];
        
    
        
        self.xsImgageUrlArray = imgageUrlArray;
    }
    
    return self;
}

- (void)getSize9_16{
    
    // 获取self.topImgBgView.frame  16:9最佳宽高
        CGFloat loveW9  = 9.0*3.0;
        CGFloat loveH16 = 16.0*3.0;
        BOOL xsIsBrek = NO;
        CGFloat topImgBgViewW = self.width-20.0-10.0;//345
        CGFloat topImgBgViewH = self.height-40;//265
        CGFloat count = 1.0;
        while (!xsIsBrek) {
                
            CGFloat tempLoveW9 = loveW9 *count;
            CGFloat tempLoveH16 = loveH16 *count;
            
            if(tempLoveW9<topImgBgViewW&&tempLoveH16<topImgBgViewH){
                count=count+1.0;
            }
            else{
                xsIsBrek = YES;
            }
            NSLog(@"loveW9==%f\n",loveW9);
            NSLog(@"loveH16==%f\n",loveH16);
        }
        count = count-1;
    // 108 192
    self.cellSize9_16 = CGSizeMake(loveW9*count, loveH16*count+40);// 135, 240
    NSLog(@"cellSize9_16背景77==%@\n",NSStringFromCGSize(self.cellSize9_16));//cellSize9_16=={324, 576}
}

- (void)setupCollectionView
{
//    // 使用系统自带的流布局（继承自UICollectionViewLayout）
//    TWLayout *layout = ({
//        TWLayout *layout = [[TWLayout alloc] init];
//        layout.itemSize                    = CGSizeMake(180, 180);
//        layout.scrollDirection             = UICollectionViewScrollDirectionHorizontal;
//        layout.minimumLineSpacing          = 10;//40;
//        
//        
//        //第一个cell和最后一个cell居中显示
//        CGFloat margin = (self.width - 160) * 0.5;
//        layout.sectionInset                = UIEdgeInsetsMake(0, margin, 0, margin);
//        
//        layout;
//    });
//    
//    // 使用UICollectionView必须设置UICollectionViewLayout属性
//    UICollectionView *collectionView = ({
//        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
//        collectionView.center            = self.center;
//        collectionView.bounds            = CGRectMake(0, 0, self.width, 200);
//        collectionView.backgroundColor   = [UIColor brownColor];
//        collectionView.dataSource        = self;
//        [collectionView setShowsHorizontalScrollIndicator:NO];
//        
//        [self addSubview:collectionView];
//        
//        collectionView;
//    });
//
//    [self registerClass:[PhotoCell class] forCellWithReuseIdentifier:cellID];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section
{
    return self.xsImgageUrlArray.count;//10;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                           cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID
                                                                           forIndexPath:indexPath];
    
//    cell.imageName = [NSString stringWithFormat:@"%ld", (long)indexPath.row];
    
    PhotoCellModel *tempModel = self.xsImgageUrlArray[indexPath.row];
    
    cell.model = tempModel;
    cell.backgroundColor = UIColor.clearColor;
    cell.contentView.backgroundColor = UIColor.clearColor;
    
    return cell;
}

- (void)xsUpdateCollectionViewData:(NSArray *)dArray{
    
    self.xsImgageUrlArray = dArray;
    [self reloadData];
}
//cell的点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.item<self.xsImgageUrlArray.count){
        PhotoCellModel *model = self.xsImgageUrlArray[indexPath.item];
        
        NSLog(@"model.url==%@\n",model.url);
       
        
        for (PhotoCellModel *tempMd in self.xsImgageUrlArray) {
            if(tempMd!=model){
                tempMd.isSelect = NO;
            }
            else{
                model.isSelect = !model.isSelect;
            }
        }
        
        if([self.AuthorityBackGroundDelegate respondsToSelector:@selector(xsAuthorityBackGroundStyleCellCallBackWithModel:)]){
            [self.AuthorityBackGroundDelegate xsAuthorityBackGroundStyleCellCallBackWithModel:model];
        }
    }
    [self reloadData];
}


//每一个分组的上左下右间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0);
}

//头部视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//    return CGSizeMake(self.frame.size.width, 45.0);
    return CGSizeMake(0.10, self.height);
}


//定义每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    //CGFloat margin = 5.0*4.0;
//    CGFloat itemW =  (self.frame.size.width-margin)/3.0;
//    CGFloat itemH = itemW*2.0;
    
    CGFloat itemW =  self.cellSize9_16.width;///3.0;//(self.frame.size.width-margin)/3.0;
    CGFloat itemH = self.cellSize9_16.height;///3.0;//itemW*(4.0/3.0);
  
    return CGSizeMake(itemW, itemH);
}


@end
